<template>
	<view class="warp">
		<!-- <view class="home_top">
			<view class="back" @click="backClick">
				<view></view>
			</view>
			<view class="title" style="font-size: 36rpx;">邀请好友</view>
		</view> -->
		<view class="info">
			<view class="image">
				<!-- <image class="suolue round" src="../../static/index/images/index_32_32.jpg"></image> -->
			</view>
			<!-- <image class="logo" src="../../static/logo.png" style="width: 250rpx;height: 250rpx;"></image> -->
			<button :disabled="!checked" class="btn1" open-type="getPhoneNumber"
				@getphonenumber="getPhoneNumber">微信用户一键登录</button>
			<!-- <view class="btn2" @click="jumpTo()">手机号码登录/注册</view> -->
		</view>
		<view class="footer flex">
			<checkbox-group @change="chang">
				<label>
					<checkbox style="transform:scale(0.6)" class="round" value="1" :checked="checked" />
				</label>
			</checkbox-group>
			<text style="color: #999999;">
				登录代表您已同意
			</text>
			力高商厨用户服务协议、隐私政策
		</view>
	</view>
</template>

<script>
	import {
		get_openid,
		getphone,
		addressResolution
	} from '../../api/api.js'
	export default {
		data() {
			return {
				data1: "",
				uid: "",
				checked: false,
			}
		},
		created() {
			let type = uni.getStorageSync('userType')
			if (!type) return;
			// 这里是获取下发权限地方，根据官方文档，可以根据  wx.getSetting() 的 withSubscriptions   这个参数获取用户是否打开订阅消息总开关。后面我们需要获取用户是否同意总是同意消息推送。所以这里要给它设置为true 。
			wx.getSetting({
				withSubscriptions: true, //  这里设置为true,下面才会返回mainSwitch
				success: function(res) {

					// 调起授权界面弹窗
					if (res.subscriptionsSetting.mainSwitch) { // 用户打开了订阅消息总开关
						if (res.subscriptionsSetting.itemSettings !=
							null) { // 用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权
							let moIdState = res.subscriptionsSetting.itemSettings[tmplIds]; // 用户同意的消息模板id
							if (moIdState === 'accept') {
								console.log('接受了消息推送');

							} else if (moIdState === 'reject') {
								console.log("拒绝消息推送");

							} else if (moIdState === 'ban') {
								console.log("已被后台封禁");

							}
						} else {
							// 当用户没有点击 ’总是保持以上选择，不再询问‘  按钮。那每次执到这都会拉起授权弹窗
							wx.showModal({
								title: '提示',
								content: '请授权开通服务通知',
								showCancel: true,
								success: function(ress) {
									if (ress.confirm) {
										wx.requestSubscribeMessage({ // 调起消息订阅界面
											tmplIds: [
												'kToPTuMBw0tokpGZmG97hFrwmxPfWsjfFB40NZ_EzkE',
												'_ca_uxU0moTLffLrskU5UZhSLyzRZV0Eude7tbcovzY',
												'kToPTuMBw0tokpGZmG97hFrwmxPfWsjfFB40NZ_EzkE'
											],
											success(res) {
												console.log('订阅消息 成功 ');
												console.log(res);
											},
											fail(er) {
												console.log("订阅消息 失败 ");
												console.log(er);
											}
										})

									}
								}
							})
						}

					} else {
						console.log('订阅消息未开启')
					}
				},
				fail: function(error) {
					console.log(error);
				},
			})

		},
		onLoad(opt) {
			if (opt.uid !== undefined) {
				uni.setStorageSync('uid', opt.uid)
				this.uid = opt.uid
				console.log('被邀请上级的id', opt.uid)
			}
			this.login()
			let that = this
			console.log('获取位置信息')
			uni.getLocation({
				type: 'wgs84',
				success(res) {
					console.log('用户位置信息', res)
					let data = {
						lng: res.longitude,
						lat: res.latitude
					}

					addressResolution(data).then(res => {
						console.log('当前位置', res.data.city)
						uni.setStorageSync('city', res.data)
						that.city = res.data.city

					})
				}
			})
			this.$forceUpdate()
		},
		methods: {
			chang(e) {
				if (e.detail.value.length) return this.checked = true
				if (!e.detail.value.length) return this.checked = false
			},
			login() {
				console.log('登录')
				let _that = this
				uni.login({
					success(res) {
						let code = res.code
						if (res.code) {
							//发起网络请求
							get_openid({
								"code": code,
							}).then(res => {
								// 请求成功后获取openid和session_key
								console.log(res.data, 'ww')
							})
						} else {
							uni.hideLoading()
							console.log('登录失败！' + res.errMsg)
						}
					},
					fail() {
						uni.hideLoading()
						console.log("获取用户信息失败");
					}
				})
			},
			// 跳转页面
			jumpTo() {
				uni.navigateTo({
					url: "/pages/phone/login"
				})
			},
			// 获取手机号
			getPhoneNumber(e) {

				// return
				if (!this.checked) {
					uni.showToast({
						title: "请阅读并勾选底部协议",
						icon: 'none'
					})
					return
				}
				this.data1 = e.detail
				console.log('第一次', e.detail)
				var _that = this;
				if (e.detail.errMsg == 'getPhoneNumber:fail user deny') { //用户点击拒绝
					wx.showToast({
						title: '取消授权!',
						duration: 5000,
						icon: 'none',
					});
				} else {

					uni.showLoading({
						title: '加载中...',
					})

					uni.login({
						success(res) {
							let code = res.code
							if (res.code) {
								//发起网络请求
								get_openid({
									"code": code,
								}).then(res => {
									// 请求成功后获取openid和session_key
									console.log(res.data, 'ww')
									let userkey = res.data
									let params = {
										// "user_id": userkey.id,
										"spread_uid": _that.uid,
										"openid": userkey.openid,
										"session_key": userkey.session_key,
										"iv": _that.data1.iv,
										"encryptedData": _that.data1.encryptedData
									}
									console.log('第二次', params)
									getphone(params).then(res => {
										console.log('res', res)
										let data = res.data
										uni.hideLoading()
										uni.setStorageSync('login', true)
										uni.setStorageSync('token', data.token)
										uni.setStorageSync('userInfo', data.userInfo)
										console.log('用户信息', uni.getStorageSync('userInfo'))
										// 请求成功后获取openid和session_key
										console.log(data)
										if (data.userInfo.is_worker == 0) {
											uni.redirectTo({
												url: '/pages/identityChoice/index'
											})
											return
										}
										uni.setStorageSync('userType', data.userInfo.is_worker)
										uni.setStorageSync('current', 0)
										uni.redirectTo({
											url: '/pages/index/index'
										})
									})

								})

							} else {
								uni.hideLoading()
								console.log('登录失败！' + res.errMsg)
							}
						},
						fail() {
							uni.hideLoading()
							console.log("获取用户信息失败");
						}
					})
				}


			},
		}
	}
</script>

<style scoped lang="less">
	.warp {
		background-color: #fff;
		// background-image: url('https://ligao.schainbaas.com/uploads/attach/2022/02/20220222/be92cd3a991c444db245c4589f444e4.png');
		background-size: 100% 100%;
		display: flex;
		justify-content: center;
		align-items: center;

		.logo {
			position: absolute;
			left: 50%;
			top: 13%;
			transform: translateY(-50%);
			transform: translateX(-50%);
		}

		.info {
			width: 70%;
			height: 40%;

			// display: flex;
			// justify-content: center;
			// align-items: center;
			.image {
				// box-shadow: 0 0 0rpx 5rpx #FFFFFF;
				padding: 1rpx;
				height: 200rpx;
				border-radius: 50%;
				display: flex;
				justify-content: center;
				align-items: center;
			}

			.btn1 {
				margin-top: 285rpx;
				color: #fff;
				font-size: 30rpx;
				background-color: #029E8E;
				height: 80rpx;
				border-radius: 40rpx;
				line-height: 80rpx;
				text-align: center;
				padding: 0rpx 0rpx;
			}

			.btn2 {
				margin-top: 50rpx;
				color: #FFFFFF;
				// font-size: 30rpx;
				// background-color: #FFFFFF;
				height: 80rpx;
				border-radius: 40rpx;
				line-height: 80rpx;
				text-align: center;
				padding: 0rpx 0rpx;
			}
		}

		.footer {
			position: absolute;
			bottom: 50rpx;
			color: #5490FE;
			width: 100%;
			text-align: center;
		}
	}
</style>